package com.richard.home.ui.fragment

import android.graphics.Rect
import android.text.Html
import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.richard.base.ext.toast
import com.richard.base.route.RouterExtra
import com.richard.base.route.RouterPath
import com.richard.base.ui.BaseVMFragment
import com.richard.base.util.ImageLoader
import com.richard.base.view.ReplyTextView
import com.richard.home.R
import com.richard.home.model.bean.PoetryDetail
import com.richard.home.ui.adapter.HomePraiseHeaderAdapter
import com.richard.home.viewModel.HomePicDetailViewModelImpl
import kotlinx.android.synthetic.main.home_pic_detail_fragment.*
import org.koin.android.viewmodel.ext.android.getViewModel
import kotlin.math.abs


@Route(path = RouterPath.Home.HOME_POETRY_DETAIL)
class HomePicDetailFragment : BaseVMFragment<HomePicDetailViewModelImpl>() {

    @JvmField
    @Autowired(name = RouterExtra.ID)
    var poetryId: Int = -1

    override fun getLayoutResId(): Int = R.layout.home_pic_detail_fragment

    override fun initView() {
        tool_bar.setBackOnClickListener(View.OnClickListener {
            pop()
        })
    }


    override fun initData() {
        mViewModel.getPoetryDetail(poetryId)

        var headerAdapter =  HomePraiseHeaderAdapter()
        clv_header.adapter = headerAdapter
        for (i in 0..5) {
            headerAdapter.addData("https://cdn.kaishuhezi.com/kstory/story/image/3f552c88-a222-4ee6-8f0f-8e78b7fc4bc9.jpg")
        }
    }



    override fun startObserve() {
        mViewModel.apply {
            uiState.observe(this@HomePicDetailFragment, Observer { poetryModel ->
                poetryModel.showError?.let { errorMsg ->
                    context?.toast(errorMsg)
                }
                poetryModel.dataModel?.let {
                    setPoetryDetail(it)
                }
            })
        }
    }

    private fun setPoetryDetail(poetry: PoetryDetail) {
        tool_bar.setTitle(poetry.name)
        tv_title.text = poetry.name
        tv_poet.text = "--${poetry.poetName} · ${poetry.dynasty}"
        tv_content.text = Html.fromHtml(poetry.content)
        tv_trans.text = Html.fromHtml(poetry.fanyi)
        tv_analysis.text = Html.fromHtml(poetry.shangxi)
        tv_about.text = Html.fromHtml(poetry.about)

        ll_about.visibility = if (poetry.about.isNullOrBlank()) View.GONE else View.VISIBLE
        ll_analysis.visibility = if (poetry.shangxi.isNullOrBlank()) View.GONE else View.VISIBLE
        ll_trans.visibility = if (poetry.fanyi.isNullOrBlank()) View.GONE else View.VISIBLE

        val url = "https://childsong.oss-cn-beijing.aliyuncs.com/jpg/comment_voice_bg.9.png"
        ImageLoader.loadNinePatchBackground(context!!, tv_content, url, R.drawable.icon_test_nine_patch, Rect(50, 10, 50, 10))

        vwv_voice.apply {
            setLines(5)
        }.start()

        ctv_content.setReplyInfo(ReplyTextView.ReplyInfo("吴波", "你好吗你好吗hello world你好吗你好吗hello world你好吗你好吗hello world你好吗你好吗hello world你好吗你好吗hello world"))
    }


    companion object {
        @JvmStatic
        fun newInstance() = HomePicDetailFragment()
    }

    override fun initVm(): HomePicDetailViewModelImpl {
        return getViewModel()
    }


}